package server

import (
	"data_service/models"
)

// NoticeService 提供公告相关业务逻辑
// 供 handler 层调用，解耦 handler 与 models
// 只负责业务流程，不直接操作数据库

type NoticeService struct{}

func NewNoticeService() *NoticeService {
	return &NoticeService{}
}

// GetLatestNotice 获取最新公告
func (s *NoticeService) GetLatestNotice() (*models.Notice, error) {
	var notice models.Notice
	err := models.GetDB().Order("created_at desc").First(&notice).Error
	if err != nil {
		return nil, err
	}
	return &notice, nil
}

// CreateNotice 创建新公告
func (s *NoticeService) CreateNotice(notice *models.Notice) error {
	return models.GetDB().Create(notice).Error
}

// GetAllNotices 获取所有公告
func (s *NoticeService) GetAllNotices() ([]models.Notice, error) {
	var notices []models.Notice
	err := models.GetDB().Order("created_at desc").Find(&notices).Error
	return notices, err
}

// DeleteNotice 删除公告
func (s *NoticeService) DeleteNotice(noticeId string) error {
	return models.GetDB().Delete(&models.Notice{}, noticeId).Error
}
